import json

import pandas as pd
from tensorflow import keras

from src.utils.config import config
from src.utils.model_utils import get_level_id_mapping,get_laptop_discont_mapping,get_laptop_discont_api_mapping,get_add_column_product_id,get_delete_column_product_id_to_train_data,get_level_template_mapping,get_level_id_mapping_df



GET_ADD_COLUMN_PRODUCT_ID=get_add_column_product_id(5)
ADD_COLUMN_GROUPS = GET_ADD_COLUMN_PRODUCT_ID.groupby(['column_name', 'default_value'])['product_key']

GET_DELETE_COLUMN_PRODUCT_ID=get_delete_column_product_id_to_train_data(5)
# 列名映射
LAPTOP_COLUMNS_MAPPING = {'内存': 'memory', '固态硬盘': 'ssd', '国内保修情况': 'guarantee', '处理器': 'CPU',
                          '机械硬盘': 'hdd', '显卡': 'GPU', '购买渠道': 'purchase_way', '键盘': 'keyboard',
                          '颜色': 'color', '屏幕类型': 'screen'}

DF_FEATURE_CLUMNS_MAPPING={'itemCode':'goods_code','levelId':'level_id','productId':'product_id','brandId':'brand_id','skuId':'sku_id'}

# 等级ID和名称映射
LAPTOP_LEVEL_ID_MAPPING = get_level_id_mapping(5)
LAPTOP_LEVEL_ID_MAPPING_DF = get_level_id_mapping_df(5)
BRAND_DISCONT_MAPPING = get_laptop_discont_mapping()
BRAND_DISCONT_API_MAPPING = get_laptop_discont_api_mapping()
LEVEL_TEMPLATE_MAPPING = get_level_template_mapping()


# 新旧等级ID和等级封箱映射
OLD_LEVEL_BIN = {361: 'level_bin_1', 299: 'level_bin_1',
                 362: 'level_bin_2',
                 122: 'level_bin_3',
                 363: 'level_bin_4',
                 123: 'level_bin_5',
                 364: 'level_bin_6',
                 124: 'level_bin_7', 365: 'level_bin_7', 368: 'level_bin_7',
                 366: 'level_bin_8', 125: 'level_bin_8', 367: 'level_bin_8',
                 126: 'level_bin_9', 369: 'level_bin_9', 127: 'level_bin_9',
                 128: 'level_bin_10', 129: 'level_bin_11', 370: 'level_bin_12', 130: 'level_bin_13'}
NEW_LEVEL_BIN = {528: 'level_bin_1', 529: 'level_bin_1',
                 505: 'level_bin_2',
                 514: 'level_bin_3',
                 515: 'level_bin_4', 516: 'level_bin_4',
                 517: 'level_bin_5',
                 518: 'level_bin_6', 519: 'level_bin_6',
                 520: 'level_bin_7', 521: 'level_bin_7', 522: 'level_bin_7',
                 523: 'level_bin_8', 524: 'level_bin_8',
                 525: 'level_bin_9', 526: 'level_bin_9', 527: 'level_bin_9',
                 530: 'level_bin_10', 531: 'level_bin_11', 532: 'level_bin_12', 533: 'level_bin_13'}
# 所有等级和封箱映射
LEVEL_BIN = OLD_LEVEL_BIN.copy()
LEVEL_BIN.update(NEW_LEVEL_BIN)
NEW_LEVEL_COEF = pd.DataFrame({
    'product_level_name': ['S', 'A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4', 'C1', 'C2', 'D1', 'D2', 'D3',
                           'E1', 'E2', 'F', 'G', 'H', 'I'],
    'coef': [1, 1, 1, 1, 0.98, 1, 1, 0.99, 1, 0.99, 0.97, 1, 0.98, 1, 0.99, 0.97, 1, 1, 1, 1]})

# 模型数据的时间跨度
MODEL_DAYS = 99
PREDICT_BATCH_SIZE=300000
RECENT_DAYS=5

# 特征变量
PRODUCT_FEATURES = ['product_name']
LEVEL_FEATURES = ['level_bin']
# LEVEL_FEATURES = ['secondary_level_id'] #使用二级等级
ATTR_FEATURES = ['product_brand_name', 'memory', 'ssd', 'guarantee', 'CPU', 'GPU', 'hdd', 'purchase_way',
                 'keyboard', 'screen']
PERIOD_FEATURES = ['period']

MODEL_FEATURES_SECOND = ['product_id','secondary_level_id','product_brand_name','memory','ssd','guarantee','CPU',
                         'GPU','hdd','purchase_way','keyboard', 'screen','period']

MODEL_FEATURES = PRODUCT_FEATURES + LEVEL_FEATURES + ATTR_FEATURES + PERIOD_FEATURES
MODEL_FEATURES_NEW = PRODUCT_FEATURES + LEVEL_FEATURES + ATTR_FEATURES + PERIOD_FEATURES

CLASSIFICATION_FEATURES = [
'product_id','secondary_level_id',
'product_brand_name','memory','ssd','guarantee','CPU','GPU','hdd','purchase_way','keyboard',
'screen',
'period',
'color',
'product_level_template_id',
'apple_year',#kr 增加特征 苹果笔记本 年份
'apple_size',#kr 增加特征 苹果笔记本 屏幕大小
'apple_is_pro',#kr 增加特征 苹果笔记本 是否为pro
]


CONTINUOUS_FEATURES = [

    'ssd_num',
    'hd_num', #机械硬盘大小 注销后效果变差
    'memory_num', #内存大小
    'product_id_num', #可能有负面影响，注销后效果变差
    # '--shop_out_num',
    'guarantee_class',
    'period_num',
'brand_amount','brand_price_kurt','brand_price_mad','brand_price_max','brand_price_mean',
'brand_price_median','brand_price_min','brand_price_skew','brand_price_std','brand_price_sum',
'product_amount',
]



ALL_FEATURES = CLASSIFICATION_FEATURES+CONTINUOUS_FEATURES

# 无属性机器检查
NO_ATTR_CHECK_LIST = ['memory', 'ssd', 'hdd', 'CPU', 'GPU']
ALL_HAVE_CHECK_LIST = ['memory', 'CPU']
HD_CHECK_LIST = ['ssd', 'hdd']

# 模型路径
MODEL_DIR = 'models/laptop/'
# 模型名称
MODEL_FILE_NAME = 'laptop_price.h5'
MODEL_MSE_FILE_NAME = 'laptop_mse_price.h5'
MOBILE_OHE_NAME = 'mobile_ohe.pkl'

MODEL_DATA = 'laptop_train_data.pkl'
# MOBILE_SCALER_NAME = 'mobile_scaler.pkl'
MOBILE_PRODUCT_OHE_NAME = 'mobile_product_ohe.pkl'
LAPTOP_STANDARD = 'laptop_standard.pkl'

BRAND_FE = 'brand_fe.pkl'
PRODUCT_FE = 'product_fe.pkl'

median_dict = 'median_dict.pkl'


# 模型保存时间
KEEP_MODEL_DAYS = 180

# 模型推送到的服务器
MODEL_PUSH_SERVERS = json.loads(config.get_config('model', 'model_servers'))
MODEL_PUSH_SERVERS_NONTIOR = json.loads(config.get_config('model', 'model_servers_nontior'))
# 模型API接口
MODEL_SERVER_PORTS = json.loads(config.get_config('model', 'model_server_ports'))

# 计算历史均价周期
LAPTOP_HISTORY_AVG_PRICE_DAYS = 7
LAPTOP_HISTORY_AVG_PRICE_PREFIX = 'lhap_'
# 历史均价缓存时长
LAPTOP_HISTORY_CACHE_TIME = 60 * 60 * 36

# 配置模型早停
# early_stop = keras.callbacks.EarlyStopping(monitor='custom_mean_absolute_percentage', patience=15)
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=30, restore_best_weights=True)
# early_stop = keras.callbacks.EarlyStopping(monitor='mean_absolute_percentage_error', patience=30, restore_best_weights=True)
# 模型callback设置
model_callbacks = [early_stop]
